package com.lanou.quanttradedata.stock.mapreduce.map;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 映射类：
 * 用来将某只股票，所有HDFS上保存的日线数据进行映射和筛选
 * 最终筛选出近某天的
 * key = 股票代码
 * value = 交易日期 开盘价 收盘价 昨日收盘价 最高价 最低价
 * 以上数据进入reduce阶段
 */
public class BiasCalculateMap extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        if(key.get() != 0) {
            //只要不是表头行，就是数据行，数据行进行map筛选部分
            //数据行样例:  a"ts_code","trade_date","open","high","low","close","pre_close","change","pct_chg","vol","amount"
            String[] line = value.toString().split(",");
            context.write(
                    new Text(line[0].substring(2).replace("\"", "")),
                    new Text(
                            line[1].replace("\"","")+" "
                                    +line[2]+" "
                                    +line[5]+" "
                                    +line[6]+" "
                                    +line[3]+" "
                                    +line[4]
                    )
            );
        }
    }
}
